﻿using System.Data;
using System.Data.OleDb;

namespace AnimeLibrary.App_Code
{
    public class ConnectionOp
    {
        //*************************************************
        //               Class Members
        //*************************************************
        #region Class Variables
        private static OleDbConnection mOleDbConnection = null;
        #endregion
        //*************************************************


        //*************************************************
        //                   Methods
        //*************************************************
        #region OleDb Methods
        /// <summary>
        /// Construct a new OleDb connection string depending on the file to be opened.
        /// </summary>
        /// <param name="filepath">The file to open.</param>
        /// <returns>The OleDb connection string to the file.</returns>
        private static string SetupOleDbConnectionString(string filepath)
        {
            OleDbConnectionStringBuilder oledb_connection_string = new OleDbConnectionStringBuilder();

            oledb_connection_string.DataSource = filepath;
            if (filepath.EndsWith(".accdb"))
            {
                oledb_connection_string.Provider = "Microsoft.ACE.OLEDB.12.0";
                oledb_connection_string.PersistSecurityInfo = false;
            }

            return oledb_connection_string.ToString();
        }

        /// <summary>
        /// Opens an OleDb Connection.
        /// </summary>
        /// <param name="databasePath">The full path of the database file to write into.</param>
        /// <returns>True if the OleDb connection has been opened.</returns>
        public static bool OpenOleDbConnection(string databasePath)
        {
            bool open = false;

            OleDbConnection = new OleDbConnection(SetupOleDbConnectionString(databasePath));

            if ((OleDbConnection != null) && (OleDbConnection.State == ConnectionState.Closed))
                OleDbConnection.Open();

            open = ((OleDbConnection != null) && (OleDbConnection.State == ConnectionState.Open));

            return open;
        }

        /// <summary>
        /// Closes the OleDb Connection.
        /// </summary>
        /// <returns>True if the OleDb connection has been closed.</returns>
        public static bool CloseOleDbConnection()
        {
            bool close = false;

            if ((OleDbConnection != null) && (OleDbConnection.State == ConnectionState.Open))
            {
                OleDbConnection.Close();

                close = true;
            }

            return close;
        }
        #endregion
        //*************************************************


        //*************************************************
        //                  Properties
        //*************************************************
        #region Properties
        public static OleDbConnection OleDbConnection
        {
            get { return mOleDbConnection; }
            set { mOleDbConnection = value; }
        }
        #endregion
        //*************************************************
    }
}